#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable:6031)

#include <iostream>
#include <string>
using namespace std;

int myAtoi(string str)
{
    string num;
    int sign = 1;
    int flag = 0;
    int i = 0;
    long long result = 0;
    while (i < str.size())
    {
        if (str[i] == '+' && flag == 0)
        {
            sign = 1;
            flag = 1;
            ++i;
        }
        else if (str[i] == '-' && flag == 0)
        {
            sign = -1;
            flag = 1;
            ++i;
        }
        else if (isdigit(str[i]))
        {
            break;
        }
        else if (str[i] == ' ')
        {
            ++i;
        }
        else
        {
            return 0;
        }
    }

    while (i < str.size() && isdigit(str[i]))
    {
        num += str[i];
        ++i;
    }

    if ((num[0] - '0') * pow(10, num.size() - 1) * sign > INT_MAX)
    {
        return INT_MAX;
    }
    if ((num[0] - '0') * pow(10, num.size()- 1) * sign < INT_MIN)
    {
        //cout << (num[0] - '0') * pow(10, num.size() - 1) << endl;
        return INT_MIN;
    }

    i = num.size() - 1;
    int j = 0;
    while (i >= 0)
    {
        result += (num[i] - '0') * pow(10, j);
        --i;
        ++j;
        if (result * sign > INT_MAX)
        {
            return INT_MAX;
        }
        if (result * sign < INT_MIN)
        {
            return INT_MIN;
        }
    }

    return result * sign;
}

int main()
{
    string s("");
    myAtoi(s);

    return 0;
}